﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace GeoSwarm_Editor
{
    public partial class ExceptionDialog : Form
    {

        private string errorReportServer = "http://siliconcluster.net/gse/error_server.php";
        private string str;

        public ExceptionDialog(Exception e)
        {
            InitializeComponent();
            str = e.Message + "\n\r\n\r" + e.StackTrace;
            this.textBox1.Text = str;
            this.Show();
        }

        private void SendReport(string reportData)
        {
            WebRequest webRequest = WebRequest.Create(this.errorReportServer);
            string parameters = "report=" + reportData + "&env=" + Environment.OSVersion.ToString() + "&dotnetenv=" + Environment.Version.ToString() + "&appver=" + Application.ProductVersion;

            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.Method = "POST";
            byte[] bytes = Encoding.ASCII.GetBytes(parameters);
            Stream os = null;
            try
            {
                webRequest.ContentLength = bytes.Length;
                os = webRequest.GetRequestStream();
                os.Write(bytes, 0, bytes.Length);
            }
            catch (Exception) { }
            finally
            {
                if (os != null)
                    os.Close();
            }
            if (os != null)
                os.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.button2.Text = "Sending Report...";
            this.button2.Enabled = false;
            this.button1.Enabled = false;
            this.SendReport(this.str);
            this.Close();
            Application.Exit();
        }
    }
}
